import { reqCateList,reqCateGoods } from "../../request/api";
// import { createSelector } from "reselect"
//初始值
const initState={
    flag:1,
    cateGoods:[],
    cateList:[],
}

//actionTypes
const types={
    CHANGE_FLAGE:"CATE_CHANGE_FLAGE",
    CHANGE_CATE_GOODS:"CATE_CHANGE_GOODS",
    CHANGE_CATE_LIST:"CATE_CHANGE_CATE_LIST",
}

//reducer 修改数据
const reducer=(state=initState,action)=>{
    switch(action.type){
        case types.CHANGE_FLAGE:
            return {
                ...state,
                flag:action.flag
            }
        case types.CHANGE_CATE_GOODS:
            return {
                ...state,
                cateGoods:action.arr
            }
        case types.CHANGE_CATE_LIST:
            return {
                ...state,
                cateList:action.arr
            }
        default:
            return state;
    }
}

//actionCreators组件派发的动作
export const actions={
    changeFlag:(flag)=>{
        return {type:types.CHANGE_FLAGE,flag}
    },
    changeCateGoods:(arr)=>{
        return {type:types.CHANGE_CATE_GOODS,arr}
    },
    changeCateList:(arr)=>{
        return {type:types.CHANGE_CATE_LIST,arr}
    },
    
    reqCateGoods:(id)=>(dispatch,getState)=>{
        reqCateGoods({fid:id}).then(res=>{
            if(res.data.code===200){
                dispatch(actions.changeCateGoods(res.data.list))
            }
        })
    },
    reqCateList:()=>(dispatch,getState)=>{
        reqCateList().then(res=>{
            if(res.data.code===200){
                dispatch(actions.changeCateList(res.data.list))
            }
        })
    }
}

//selectors导出数据
export const getCateGoods=(state)=>state.cate.cateGoods
export const getCateList=(state)=>state.cate.cateList
export const getFlag=(state)=>state.cate.flag

export default reducer;
